using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using AIFSCMSClient;
using AIFSContentManagementSystem.Articles;
using AIFSContentManagementSystem.Criteria;
using AIFSContentManagementSystem.Articles.Messages;
using AIFSContentManagementSystem.Criteria.Messages;

#if VS2005
    //using UserOfAIFS2005.AIFSCMSService;
#else
    //using UserOfAIFS.AIFSCMSService;
#endif

namespace UserOfAIFS
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Use the rule builder
            //RuleExpressionBuilder ruleBuilder = ExpessionBuilderFactory.GetRuleExpressionBuilder();
            //string[] exp = ruleBuilder.CreateStringCriteria("COUNTRY").IsEqualTo("US").AndNumericCriteria("POLICY_YEAR").IsGreaterThanEqualTo("2011").CriteriaExpression;
            //WebserviceCall();

            LibraryCall();
        }

        //private void WebserviceCall()
        //{
        //    AIFSCMSServiceProvider provider = new AIFSCMSServiceProvider();
        //    provider.Timeout = 5000000;

        //    GetFilteredArticleListRequest request = new GetFilteredArticleListRequest();
        //    request.SearchParameter = new ArticleSearchParameter();
        //    request.SearchParameter.ArticleType = ArticleType.PROMO;
        //    request.SearchParameter.Program = "COLLEGE";
        //    request.SearchParameter.ActiveFlag = true;

        //    FactExpressionBuilder builder = ExpessionBuilderFactory.GetFactExpressionBuilder();

        //    string[] expression = builder.CreateStringCriteria("COUNTRY").IsEqualTo("US")
        //        .AndNumericCriteria("POLICY_YEAR").IsEqualTo("2011").CriteriaExpression;

        //    Criterion criteria = new Criterion();
        //    criteria.CriteriaExpressionArray = expression;

        //    Criterion orCountryCriteria = new Criterion();
        //    expression = builder.CreateStringCriteria("SPONSOR").IsEqualTo("468")
        //        .AndDateCriteria("POLICY_YEAR").IsEqualTo("2011").CriteriaExpression;

        //    orCountryCriteria.CriteriaExpressionArray = expression;

        //    List<Criterion> criteriaList = new List<Criterion>();
        //    criteriaList.Add(criteria);
        //    criteriaList.Add(orCountryCriteria);
        //    request.Criteria = criteriaList.ToArray();

        //    GetFilteredArticleListResponse result = (GetFilteredArticleListResponse)provider.ProcessArticleRequest(request);
        //    if (result.HasError)
        //    {
        //        //Response.Write(String.Join(".", result.ErrorMessage));
        //        return;
        //    }

        //    if (result.HasDataInReturn)
        //    {
        //        rptrPromo.DataSource = result.ArticleList;
        //        rptrPromo.DataBind();
        //    }
        //}

        protected void rptrPromo_OnItemDataBound(Object sender, RepeaterItemEventArgs e)
        {
            switch (e.Item.ItemType)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    Repeater rptrFragment = e.Item.FindControl("rptrFragment") as Repeater;
                    if (rptrFragment != null)
                    {
                        Article article = e.Item.DataItem as Article;
                        if(article != null)
                        {
                            ///TODO: Replace place holders
                            rptrFragment.DataSource = article.Fragments;
                            rptrFragment.DataBind();
                        }                        
                    }

                    break;
            }
        }

        private void LibraryCall()
        {
            GetFilteredArticleListRequest request = new GetFilteredArticleListRequest();
            request.SearchParameter = new ArticleSearchParameter();
            request.SearchParameter.ArticleType = ArticleType.PROMO;
            request.SearchParameter.Program = "COLLEGE";
            request.SearchParameter.ActiveFlag = true;

            FactExpressionBuilder builder = ExpessionBuilderFactory.GetFactExpressionBuilder();

            string[] expression = builder.CreateStringCriteria("COUNTRY").IsEqualTo("US")
                .AndNumericCriteria("POLICY_YEAR").IsEqualTo("2011").CriteriaExpression;

            Criterion criteria = new Criterion();
            criteria.CriteriaExpressionArray = expression;

            Criterion orCountryCriteria = new Criterion();
            expression = builder.CreateStringCriteria("SPONSOR").IsEqualTo("468")
                .AndDateCriteria("POLICY_YEAR").IsEqualTo("2011").CriteriaExpression;

            orCountryCriteria.CriteriaExpressionArray = expression;

            List<Criterion> criteriaList = new List<Criterion>();
            criteriaList.Add(criteria);
            criteriaList.Add(orCountryCriteria);
            request.Criteria = criteriaList;

            AIFSCMSFacade.IFacadeContext facadeContext = new ConnectedFacadeContext();
            AIFSCMSFacade.AifsCmsFacade facade = new AIFSCMSFacade.AifsCmsFacade(facadeContext);

            GetFilteredArticleListResponse result = (GetFilteredArticleListResponse)facade.ProcessArticleRequest(request);
            if (result.HasError)
            {
                //Response.Write(String.Join(".", result.ErrorMessage));
                return;
            }

            if (result.HasDataInReturn)
            {
                rptrPromo.DataSource = result.ArticleList;
                rptrPromo.DataBind();
            }
        }
    }
}
